/**
 * @File:    example_flexible_button.c
 * @Author:  MurphyZhao
 * @Date:    2018-09-29
 * 
 * Copyright (c) 2018-2018 MurphyZhao <d2014zjt@163.com>
 *               https://github.com/zhaojuntao
 * All rights reserved.
 * License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Change logs:
 * Date        Author       Notes
 * 2018-09-29  MurphyZhao   First add
 * 
*/

#include <rtthread.h>
#include "flexible_button.h"
#include <stdint.h>
#include "main.h"
#include "water_quality.h"

/* ������ƿ� */
struct rt_mailbox key_mb;
/* ���ڷ��ʼ����ڴ�� */
static char keymb_pool[64];
static flex_button_t button;
static uint8_t key_state;

extern void Wifi_SendF2_Info(void);
extern void WVV_Ctrl(int num, int action) ;

static void btn_callback(flex_button_t *btn)
{

    //static rt_uint32_t touchnum;
    if (btn == &button)
    {
        switch (btn->event)
        {
        case FLEX_BTN_PRESS_DOWN:
            rt_kprintf("button [FLEX_BTN_PRESS_DOWN]\n");
            break;
        case FLEX_BTN_PRESS_CLICK:
            
			TOC_Action(TOC_MEASURE_FLAG);

        break;
        case FLEX_BTN_PRESS_DOUBLE_CLICK:
        
 			TOC_Action(TOC_ADJUST_FLAG);
            break;
        case FLEX_BTN_PRESS_SHORT_START:
            rt_kprintf("button [FLEX_BTN_PRESS_SHORT_START]\n");
            break;
        case FLEX_BTN_PRESS_SHORT_UP:
            rt_kprintf("button [FLEX_BTN_PRESS_SHORT_UP]\n");
            break;
        case FLEX_BTN_PRESS_LONG_START:
        	WQ_Reset();  
// 			TOC_Action(TOC_ADJUST_FLAG);
            rt_kprintf("button [FLEX_BTN_PRESS_LONG_START]\n");
            break;
        case FLEX_BTN_PRESS_LONG_UP:
            rt_kprintf("button [FLEX_BTN_PRESS_LONG_UP]\n");
            break;
        case FLEX_BTN_PRESS_LONG_HOLD:
            rt_kprintf("button [FLEX_BTN_PRESS_LONG_HOLD]\n");
            break;
        case FLEX_BTN_PRESS_LONG_HOLD_UP:
            rt_kprintf("button [FLEX_BTN_PRESS_LONG_HOLD_UP]\n");
            break;
        }
    }
}

static uint8_t button_read(void)
{
    key_state = (uint8_t)HAL_GPIO_ReadPin(KEY_GPIO_Port, KEY_Pin);
    return key_state;
}
static void button_scan(void *arg)
{
    while (1)
    {
        flex_button_scan();
        rt_thread_mdelay(20);
    }
}

static void user_button_init(void)
{
    //register zgb_key
    button.usr_button_read = button_read;
    button.cb = (flex_button_response_callback)btn_callback;
    button.status = 0;
    button.pressed_logic_level = 0;
    button.click_start_tick = 20;
    button.short_press_start_tick = 100;
    button.long_press_start_tick = 200;
    button.long_hold_start_tick = 300;
    flex_button_register(&button);
}
static struct rt_thread flex_btn_tid;
static rt_uint8_t flex_btn_thread_stack[512];
#define BTN_THREAD_PRIORITY 10
#define BTN_THREAD_TICK 20
int flex_button_main(void)
{
    user_button_init();
    /* ��ʼ��һ��mailbox */
    rt_mb_init(&key_mb,
               "mbt",                  /* ������mbt */
               &keymb_pool[0],         /* �����õ����ڴ����mb_pool */
               sizeof(keymb_pool) / 4, /* ��С��mb_pool/4����Ϊÿ���ʼ��Ĵ�С��4�ֽ� */
               RT_IPC_FLAG_FIFO);      /* ����FIFO��ʽ�����̵߳ȴ� */
    rt_thread_init(&flex_btn_tid, "flex_btn", button_scan,
                       RT_NULL, &flex_btn_thread_stack[0], sizeof(flex_btn_thread_stack),
                       BTN_THREAD_PRIORITY, BTN_THREAD_TICK);

    rt_thread_startup(&flex_btn_tid);
    return 0;
}
//INIT_APP_EXPORT(flex_button_main);
